package com.xingen.utils;

import com.xingen.domain.TableFileds;
import com.xingen.domain.Tables;
import com.xingen.mapper.QueryMapper;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * 创建word文档 步骤: 1,建立文档 2,创建一个书写器 3,打开文档 4,向文档中写入数据 5,关闭文档
 *
 * @Auther: 辛根
 * @Date: 2020/04/17/0:03
 * @Description:
 */
@Service
public class DateToWordUtil {

    @Autowired
    QueryMapper queryMapper;


    public void toWord(List<Tables> tables, String fileName) {// 创建word文档,并设置纸张的大小
        Document document = new Document(PageSize.A4);
        try {
            File file = new File(fileName);
            // 2 如果目标路径不存在 则新建该路径
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
//            if (!file.exists()){
//                file.createNewFile();
//            }
            // 创建word文档
            RtfWriter2.getInstance(document, new FileOutputStream(file));
            document.open();
            Paragraph ph = new Paragraph();

            RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1;
            rtfGsBt1.setAlignment(Element.ALIGN_CENTER);
            rtfGsBt1.setStyle(Font.NORMAL);
            rtfGsBt1.setSize(30);
            Paragraph p = new Paragraph("数据库文档", rtfGsBt1);

            document.add(p);

            for (int i = 0; i < tables.size(); i++) {
                String table_name = tables.get(i).getName();
                String table_comment = tables.get(i).getComment();

                List<TableFileds> fileds = queryMapper.getTable(tables.get(i).getName());

//                String all = "" + (i + 1) + " 表名:" + table_name + ":" + table_comment;//表头
                String all = "" + (i + 1) + ":" + table_comment + ":" + table_name;//表头

                Table table = new Table(6);//6列
                table.setWidths(new int[]{10, 15, 15, 10, 10, 30});
                table.setAlignment(Element.ALIGN_CENTER);
                document.add(new Paragraph(""));

                table.setBorderWidth(1);

                table.setPadding(0);

                table.setSpacing(0);

                /*
                 * 添加表头的元素，并设置表头背景的颜色
                 */
                Color chade = new Color(176, 196, 222);

                Cell cell = new Cell("行号");// 单元格

                cell.setBackgroundColor(chade);

                cell.setHeader(true);

                table.addCell(cell);

                cell = new Cell("字段");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);

                cell = new Cell("字段类型");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);

                cell = new Cell("可否为空");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);

                cell = new Cell("是否主键");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);

                cell = new Cell("描述");// 单元格

                cell.setBackgroundColor(chade);

                table.addCell(cell);

                table.endHeaders();// 表头结束
                // 表格的主体
                for (int k = 0; k < fileds.size(); k++) {
                    String Field = fileds.get(k).getField();

                    String Type = fileds.get(k).getType().replace("unsigned", "");

                    String Null = fileds.get(k).getNull();

                    String Key = fileds.get(k).getKey().equals("PRI") ? "是" : "否";

                    String Comment = fileds.get(k).getComment();
                    table.addCell((k + 1) + "");

                    table.addCell(Field);

                    table.addCell(Type);

                    table.addCell(Null);

                    table.addCell(Key);

                    table.addCell(Comment);
                }
                RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2;
                rtfGsBt2.setAlignment(Element.ALIGN_LEFT);
                rtfGsBt2.setStyle(Font.NORMAL);
                rtfGsBt2.setSize(18);
                Paragraph pheae = new Paragraph(all, rtfGsBt2);
                //写入表说明
                document.add(pheae);
                //生成表格
                document.add(table);
            }
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
